import { RandomUtils } from "./Trigon/Random";
import Zombie, { ZombieState } from "./Zombie";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
	@property(cc.Prefab)
	zombiePrefab: cc.Prefab = null;

	// onLoad () {}

	start() {
		this.schedule(this.createZombieArray.bind(this), 1, cc.macro.REPEAT_FOREVER, 2);
	}

	createZombieArray() {
		let r = RandomUtils.rangeInt(2, 5);
		for (let i = 0; i < r; i++) {
			let x =  RandomUtils.rangeInt(-this.node.width / 2, this.node.width / 2);
			let y =  RandomUtils.rangeInt(-this.node.height / 2, this.node.height / 2);

			if (Math.random() < 0.5) {
				x = (Math.sign(x) * this.node.width) / 2;
			} else {
				y = (Math.sign(y) * this.node.height) / 2;
			}
            let zombie = this.createZombie();
            if(zombie){
                zombie.x = x;
                zombie.y = y;
            }
		}
	}

	createZombie() {
		let zombie = cc.instantiate(this.zombiePrefab);
        if(Math.random()<0.2){
            let z=zombie.getComponent(Zombie);
            z.state=ZombieState.pink;
        }
		this.node.addChild(zombie);
		return zombie;
	}

	// update (dt) {}

	
}
